import os
import uuid
import random

import bpy

from ..utils.log import log

ASSET_PATH = r"D:\Library\blender\render_master"
TMP = r"C:\tmp"
OBJECT_OUT = TMP + r"\object"
SCENE_OUT = TMP + r"\scene"
POOL_OUT = TMP + r"\pool"


def set_out_path(path):
    if not os.path.isdir(path):
        os.makedirs(path)
    bpy.context.scene.render.filepath = path
    log.debug(f"set_out_path: {path}", )


def test_class_list(cls):
    for cl in cls:
        c = cl()
        start = getattr(c, "start_test", None)
        if start:
            start()


def new_tag_empty():
    empty = bpy.data.objects.new("TAG-" + str(uuid.uuid1()), None)
    empty.empty_display_type = "CUBE"
    bpy.context.scene.collection.objects.link(empty)
    return empty


def random_transform(obj):
    with bpy.context.temp_override(selected_objects=[obj, ], active_object=obj):
        bpy.ops.object.randomize_transform(
            loc=(5, 5, 5),
            rot=(0.285361, 0.292168, 0.679631),
            random_seed=random.randint(0, 514),
        )


def clear_scene():
    """清理场景"""
    new = bpy.data.scenes.new("new")
    for sc in bpy.data.scenes:
        if sc != new:
            bpy.data.scenes.remove(sc)
    bpy.ops.outliner.orphans_purge(do_local_ids=True, do_linked_ids=True, do_recursive=True)
